package com.example.newproject_overseas

import android.content.Context
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import com.example.composewebview.Utils.PermissionUtil.checkCameraPermission
import com.example.composewebview.Utils.PermissionUtil.checkPhotosPermission
import com.example.composewebview.Utils.PermissionUtil.checkStoragePermission
import com.example.composewebview.Utils.PermissionUtil.checkStorageReadPermission
import com.example.composewebview.Utils.PermissionUtil.checkStorageWritePermission
import com.example.composewebview.Utils.PermissionUtil.reqPermission
import com.example.newproject_overseas.components.AppNav
import com.example.newproject_overseas.ui.theme.NewProject_overseasTheme
import com.example.newproject_overseas.util.getToken

class MainActivity : ComponentActivity() {

    lateinit var ctx_sPerLauncher: ActivityResultLauncher<String>        // 声明为成员变量
    lateinit var ctx_mPerLauncher: ActivityResultLauncher<Array<String>> // 声明为成员变量

    private val sPerLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
        // 处理单个权限请求的结果
        if (isGranted) {
            // 权限已授予
        } else {
            // 权限被拒绝
        }
    }

    private val mPerLauncher = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
        // 处理多个权限请求的结果
        for ((permission, isGranted) in permissions) {
            if (isGranted) {
                // 权限已授予
            } else {
                // 权限被拒绝
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            NewProject_overseasTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    AppNav()
                }
            }
        }
        ctx_sPerLauncher = sPerLauncher;
        ctx_mPerLauncher = mPerLauncher;
        //权限请求
        permissionRequired(this)
    }


    fun permissionRequired(context: Context){
        var launcher: ActivityResultLauncher<Any>;
        val checkCameraPer  = checkCameraPermission(context)
        val checkPhotosPer  = checkPhotosPermission(context)
        val checkStoragePer = checkStoragePermission(context);
        val checkStorageRPer = checkStorageReadPermission(context)
        val checkStorageWPer = checkStorageWritePermission(context)


        if(!checkCameraPer){
            launcher = ctx_sPerLauncher as ActivityResultLauncher<Any>
            reqPermission("camera",launcher) { isAuthorized ->

            }
        }

        if(!checkPhotosPer){
            launcher = ctx_sPerLauncher as ActivityResultLauncher<Any>
            reqPermission("photos",launcher) { isAuthorized ->

            }
        }

        if(!checkStoragePer){
            launcher = ctx_mPerLauncher as ActivityResultLauncher<Any>
            reqPermission("storage",launcher) { isAuthorized ->

            }
        }

        if(!checkStorageRPer){
            launcher = ctx_sPerLauncher as ActivityResultLauncher<Any>
            reqPermission("storage-read",launcher) { isAuthorized ->

            }
        }

        if(!checkStorageWPer){
            launcher = ctx_sPerLauncher as ActivityResultLauncher<Any>
            reqPermission("storage-write",launcher) { isAuthorized ->

            }
        }

    }
}

